package com.zifeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zifeng.common.Query;
import com.zifeng.dao.OrderDao;
import com.zifeng.pojo.Order;
import com.zifeng.service.IOrderService;
import com.zifeng.utils.DateUtils;
import com.zifeng.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

/**
 * 船舶订单Service业务层处理
 * 
 * @author zifeng
 * @date 2025-03-27
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao, Order> implements IOrderService
{
    @Autowired
    private OrderDao orderDao;

    @Override
    public Long countByMonth(Integer year, Integer month) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        String startDate = String.format("%04d-%02d-01 00:00:00", year, month);
        String endDate = String.format("%04d-%02d-%02d 23:59:59", year, month, DateUtils.getLastDayOfMonth(year, month));
        queryWrapper.between("create_at", startDate, endDate);
        return orderDao.selectCount(queryWrapper);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        Object userId = params.get("userId");
        if(userId != null) {
            int newUserId = Integer.parseInt((String) userId);
            wrapper.eq("master_id", newUserId).or().eq("client_id", newUserId);
        }
        IPage<Order> page = this.page(
                new Query<Order>().getPage(params),
                wrapper
        );


        return new PageUtils(page);
    }


    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Order selectOrderById(Long id)
    {
        return orderDao.selectById(id);
    }


    /**
     * 新增【请填写功能名称】
     * 
     * @param order 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        return orderDao.insert(order);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param order 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        return orderDao.updateById(order);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(Long[] ids)
    {
        return orderDao.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteOrderById(Long id)
    {
        return orderDao.deleteById(id);
    }
}
